﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using iss.sg4210.se18.team6s.jass.BL;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.Models;
using iss.sg4210.se18.team6s.Utilities;

namespace iss.sg4210.se18.team6s.Controllers
{
    [Authorize(Roles = ViewConstants.ROLE_JOB_SEEKER + "," + ViewConstants.ROLE_ADVERTISER + "," + ViewConstants.ROLE_ADMIN)]
    public class ResponseController : Controller
    {
        ResponsesFacade responseFacade = null;
        AdvertisementFacade advertisementFacade = null;
        public ResponseController()
        {
            responseFacade = new ResponsesFacade();
            advertisementFacade = new AdvertisementFacade();
        }
        //
        // GET: /Responses/

        public ActionResult Index()
        {
            if (Common.IsLoggedInUserAdmin())
                return View(responseFacade.FindAllResponses());
            else if (Common.IsLoggedInUserAdvertiser())
                return View(responseFacade.FindAllResponsesByAdvertisement(Common.GetLoggedInUserId()));
            else
                return View(responseFacade.FindAllResponsesByUser(Common.GetLoggedInUserId()));
        }

        //
        // GET: /Responses/Details/5

        public ActionResult Details(int id)
        {
            return View(responseFacade.GetResponseById(id));
        }

        //
        // GET: /Responses/Create/id

        public ActionResult Create(int id)
        {
            // TODO: Add insert logic here
            Advertisement advertisement = advertisementFacade.GetAdvertisement(id);
            RespondModel respondModel = new RespondModel();

            respondModel.AdvertisementName = advertisement.Title;
            MembershipUser user = Membership.GetUser(true);
            respondModel.Name = user.UserName;
            respondModel.Email = user.Email;

            return View(respondModel);
        }

        //
        // POST: /Responses/Create

        [HttpPost]
        public ActionResult Create(int id, FormCollection collection)
        {
            RespondModel respondModel = null;
            try
            {
                // TODO: Add insert logic here
                respondModel = new RespondModel();
                UpdateModel(respondModel, collection);

                Response response = new Response();
                response.AdvertisementId = id;
                response.UserId = Common.GetLoggedInUserId();
                response.Address = respondModel.Address;
                response.Email = respondModel.Email;
                response.Name = respondModel.Name;
                response.Resume = respondModel.Resume;
                response.Telephone = respondModel.Telephone;

                responseFacade.AddResponse(response);

                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {

                return View(respondModel);
            }
        }

        //
        // GET: /Responses/Edit/5

        public ActionResult Edit(int id)
        {
            return View(responseFacade.GetResponseById(id));
        }

        //
        // POST: /Responses/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Response response = null;
            try
            {
                // TODO: Add update logic here
                response = responseFacade.GetResponseById(id);
                UpdateModel(response);
                responseFacade.UpdateResponse(response);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(response);
            }
            catch (Exception ex)
            {
                return View(response);
            }
        }

        //
        // GET: /Responses/Delete/5

        public ActionResult Delete(int id)
        {
            return View(responseFacade.GetResponseById(id));
        }

        //
        // POST: /Responses/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                responseFacade.DeleteResponse(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(responseFacade.GetResponseById(id));
            }
            catch (Exception ex)
            {

                return View(responseFacade.GetResponseById(id));
            }
        }
    }

    [Authorize(Roles = ViewConstants.ROLE_ADMIN + "," + ViewConstants.ROLE_ADVERTISER)]
    public class ResponseActionController : Controller
    {
        ResponsesFacade responseFacade = null;
        public ResponseActionController()
        {
            responseFacade = new ResponsesFacade();
        }

        //
        // GET: /ResponseAction/

        public ActionResult Index()
        {
            if (Common.IsLoggedInUserAdmin())
                return View(responseFacade.FindAllResponseActions());
            else
                return View(responseFacade.FindAllResponsesActionsByAdvertisement(Common.GetLoggedInUserId()));
        }

        //
        // GET: /ResponseAction/Details/5

        public ActionResult Details(int id)
        {
            if (Common.IsLoggedInUserAdmin())
                return View(responseFacade.GetResponseAction(id));
            else
                return View(responseFacade.GetResponseAction(id, Common.GetLoggedInUserId()));
        }

        //
        // GET: /ResponseAction/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ResponseAction/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                ResponseAction responseAction = new ResponseAction();
                UpdateModel(responseAction, collection);
                responseFacade.AddResponseAction(responseAction);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /ResponseAction/Edit/5

        public ActionResult Edit(int id)
        {
            return View(responseFacade.GetResponseAction(id));
        }

        //
        // POST: /ResponseAction/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            ResponseAction responseAction = responseFacade.GetResponseAction(id);
            try
            {
                // TODO: Add update logic here
                iss.sg4210.se18.team6s.jass.DAL.Action action = responseFacade.GetAction(Convert.ToInt32(collection["ActionId"].Split(',')[1]));
                responseAction.ActionId = action.Id;
                responseAction.Notes = collection["Notes"];
                //UpdateModel(responseAction);
                responseFacade.UpdateResponseAction(responseAction);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(responseAction);
            }
            catch (Exception ex)
            {
                return View(responseAction);
            }
        }

        //
        // GET: /ResponseAction/Delete/5

        public ActionResult Delete(int id)
        {
            return View(responseFacade.GetResponseAction(id));
        }

        //
        // POST: /ResponseAction/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                responseFacade.DeleteResponseAction(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(responseFacade.GetResponseAction(id));
            }
            catch (Exception ex)
            {

                return View(responseFacade.GetResponseAction(id));
            }
        }

        public ActionResult ActionList(int id)
        {
            if (id < 0)
                ViewData[ViewConstants.VIEW_DATA_ACTION_LIST] = new SelectList(responseFacade.FindAllActions(), "Id", "Name");
            else
                ViewData[ViewConstants.VIEW_DATA_ACTION_LIST] = new SelectList(responseFacade.FindAllActions(), "Id", "Name", responseFacade.GetAction(id).Id);

            return View("ActionDropDownList");
        }

        public ActionResult ResponseList(int id)
        {
            if (id < 0)
                ViewData[ViewConstants.VIEW_DATA_RESPONSE_LIST] = new SelectList(responseFacade.FindAllResponses(), "Id", "Name");
            else
                ViewData[ViewConstants.VIEW_DATA_RESPONSE_LIST] = new SelectList(responseFacade.FindAllResponses(), "Id", "Name", responseFacade.GetResponse(id).Id);

            return View("ResponseDropDownList");
        }
    }

    [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
    public class ActionController : Controller
    {
        ResponsesFacade responseFacade = null;
        public ActionController()
        {
            responseFacade = new ResponsesFacade();
        }

        //
        // GET: /Action/

        public ActionResult Index()
        {
            return View(responseFacade.FindAllActions());
        }

        //
        // GET: /Action/Details/5

        public ActionResult Details(int id)
        {
            return View(responseFacade.GetAction(id));
        }

        //
        // GET: /Action/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Action/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                iss.sg4210.se18.team6s.jass.DAL.Action action = new iss.sg4210.se18.team6s.jass.DAL.Action();
                UpdateModel(action, collection);
                responseFacade.AddAction(action);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Action/Edit/5

        public ActionResult Edit(int id)
        {
            return View(responseFacade.GetAction(id));
        }

        //
        // POST: /Action/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            iss.sg4210.se18.team6s.jass.DAL.Action action = responseFacade.GetAction(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(action);
                responseFacade.UpdateAction(action);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(action);
            }
            catch (Exception ex)
            {
                return View(action);
            }
        }

        //
        // GET: /Action/Delete/5

        public ActionResult Delete(int id)
        {
            return View(responseFacade.GetAction(id));
        }

        //
        // POST: /Action/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                responseFacade.DeleteAction(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(responseFacade.GetAction(id));
            }
            catch (Exception ex)
            {

                return View(responseFacade.GetAction(id));
            }
        }
    }


}
